import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import Layout from '../views/Layout.vue'
import store from '@/store/index.js';
import axios from 'axios'
/**
 * 静态路由
 */
export const constantRouterMap = [
    {
        path: '/login',
        name: 'login',
        hidden:true,
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/',
        name: 'Layout',
        redirect: '/index',
        meta: {
            title: '首页'
        },
        component: Layout,
        children: [
            {
                path: '/index',
                name: 'index',
                meta: {
                    title: '首页'
                },
                component: () => import('@/views/index.vue')
            }
        ]
    },
    {
        	meta: {
        		"title": "OA"
        	},
        	path: "",
            component: Layout,
        	children:[{
        		meta: {
        		title: "用户管理"
        		},
        		path: "/user/userlist/",
                component:()=>import('@/views/user/userlist.vue')
        	}]
        }
    // {
	// 	meta: {
	// 		"title": "管理中心"
	// 	},
	// 	path: "",
    //     component: Layout,
	// 	children:[{
	// 		meta: {
	// 		title: "菜单管理"
	// 		},
	// 		path: "/manager_center/menu_manager/",
    //         component:()=>import('@/views/manager_center/menu_manager.vue')
	// 	}, {
	// 		meta: {
	// 		title: "功能列表"
	// 		},
	// 		path: "/manager_center/ability_list/",
    //         component:()=>import('@/views/manager_center/ability_list.vue')
	// 	}]
	// }
]
/**
 * 需要动态添加的路由
 */

//
function saveObjArr(name, data) { //localStorage 存储数组对象的方法
    localStorage.setItem(name, JSON.stringify(data))
}

function getObjArr(name) { //localStorage 获取数组对象的方法
    return JSON.parse(localStorage.getItem(name));
}

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    // history:createWebHashHistory(),
    routes: constantRouterMap
})

router.beforeEach(async(to, from, next) => {
    const token =getObjArr("token");
    console.log("路由首位---token",token) 
    if (token) {
        // if(getObjArr("menu")){
        //     console.log("vuex菜单数组是否存在", store.state.menu.length)
        //     console.log("我要追加路由")
        //    store.dispatch("addUserMenus",getObjArr("menu"))
        // }
         console.log("vuex菜单数组是否存在", store.state.menu.length)
        if (store.state.menu.length == 0) {
            store.commit("add_include_component",getObjArr("inclue_arr") || [])
            const res = await store.dispatch("get_menu")
            console.log("res",res)
            store.dispatch("addUserMenus", res).then((msg) => {
                console.log("动态路由添加完成")
                next({ ...to, replace: true })
                // if(!res.length){
                //     next()
                // }else{
                //     next({ ...to, replace: true })
                // }
                
            })
        } else {
            next()
        }
    } else {
        if (to.path == '/login') {
            next()
        } else {
            next('/login')
        }
    }
})


export default router;
